package com.boboPlanet.controller;

import com.alibaba.fastjson.JSON;
import com.boboPlanet.comm.exception.BusinessException;
import com.boboPlanet.comm.result.ResponseResult;
import com.boboPlanet.entity.po.GatewayPo;
import com.boboPlanet.entity.vo.AllowPathVo;
import com.boboPlanet.service.GatewayService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Collections;
import java.util.List;

/**
 * @Description: TODO 初始化配置
 * @Author: mis_wu
 * @Date: 2022/7/10
 **/
@RestController
@RequestMapping("/gatewayConfig")
@Api(tags = "网关路由配置控制器")
@Slf4j
@Validated
public class GatewayConfigController {

    @Autowired
    private GatewayService gatewayService;

    /**
     * 添加网关路由配置
     * 注解校验不生效
     * @param gatewayPo gatewayPo
     * @return Boolean
     */
    @PostMapping("/insertGatewayRoute")
    @ApiOperation("添加网关路由配置")
    public ResponseResult<Boolean> insertGatewayRoute(@RequestBody GatewayPo gatewayPo){
        log.info("网关添加路由配置参数:{}",gatewayPo);
        if (ObjectUtils.isEmpty(gatewayPo)){
            throw BusinessException.build("网关路由配置参数不能为空");
        }
        if (StringUtils.isEmpty(gatewayPo.getRouteId()) || StringUtils.isEmpty(gatewayPo.getRouteUrl())){
            throw BusinessException.build("网关路由id 或 路由服务 不能为空");
        }
        if (StringUtils.isEmpty(gatewayPo.getRouteName())){
            gatewayPo.setRouteName(gatewayPo.getRouteId());
        }
        if (StringUtils.isEmpty(gatewayPo.getPredicatesType()) || ObjectUtils.isEmpty(gatewayPo.getPredicatesValueArr())){
            gatewayPo.setPredicatesType("Path");
            //对PredicatesValueArr进行操作,插入数据库时使用PredicatesValue
            gatewayPo.setPredicatesValueArr(Collections.singletonList("/gatewayApi/**"));
        }
        if (StringUtils.isEmpty(gatewayPo.getFilterName()) || StringUtils.isEmpty(gatewayPo.getFilterValue())){
            gatewayPo.setPredicatesType("StripPrefix");
            gatewayPo.setPredicatesValue("0");
        }
        if (StringUtils.isEmpty(gatewayPo.getRouteType())){
            gatewayPo.setRouteType("0");
        }
        if (ObjectUtils.isEmpty(gatewayPo.getIsOpen())){
            gatewayPo.setIsOpen(0);
        }
        return gatewayService.insertGatewayConfig(gatewayPo);
    }

    @GetMapping("/getAllAllowPath/{page}/{pageSize}")
    @ApiOperation("分页获取允许filter通过路由")
    public ResponseResult<List<AllowPathVo>> getAllAllowPath(String pathValue, @PathVariable Integer page, @PathVariable Integer pageSize){
        log.info("获取允许通过网关路径参数:{},分页：{}-{}",pathValue,page,pageSize);
        return gatewayService.getAllAllowPath(pathValue,page,pageSize);
    }

    @PostMapping("/addAllowPath")
    @ApiOperation("添加允许filter通过路由")
    public ResponseResult<Boolean> addAllowPath(@RequestBody String path){
        Object pathVal = JSON.parseObject(path).get("path");
        log.info("add 允许的路径:{}",pathVal);
        return ResponseResult.isSuccess(gatewayService.addAllowPath(pathVal.toString()));
    }

}
